﻿/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

/// <reference path="./window.js" />
/// <reference path="../Libs/Mootools-Core.js" />
/// <reference path="../Libs/Mootools-More.js" />

/*
Class: SelectSectorWindow
Shows the user a window for selection a new sector.
*/
var SelectSectorWindow = {};

/*
Function: Init
Initializes the inner components (the base UIWindow).
*/
SelectSectorWindow.Init = function()
{
  var content = "<h3>Choose a Sector</h3>"
              + "<form id='SelectSectorForm'>"
              + "<span>Name: </span><input type='text' id='SelectSectorName' class='required minLength:3 maxLength:30 alphanum' maxlength='30' /><br />"
              + "<select id='SelectSectorSelect' size='8'>"
              + "</select><br />"
              + "<span>Filter: </span><input type='text' id='SelectSectorFilter' onkeyup='SelectSectorWindow.Filter();' /><br /><br />"
              + "<input type='button' value='Select' onclick='SelectSectorWindow.Select();'>"
              + "</form>";

  SelectSectorWindow.Window = new UIWindow({
    name: "SelectSectorWindow",
    caption: "Select Sector",
    content: content,
    modal: true
  });

  SelectSectorWindow.Validator = new Form.Validator.Tips($("SelectSectorForm"), { scrollToErrorsOnSubmit: false });
}

/*
Function: Show
Shows the window.
*/
SelectSectorWindow.Show = function(callback)
{
  if (!this.Window)
    this.Init();

  this.Window.show();
  $("SelectSectorName").set("value", "");
  $("SelectSectorFilter").set("value", "");
  this.Filter();
  this.Callback = callback;
}

/*
Function: Hide
Hides the window.
*/
SelectSectorWindow.Hide = function()
{
  this.Window.hide();
}

/*
Function: Filter
Applies a filter to the list of available sectors.
*/
SelectSectorWindow.Filter = function()
{
  var select = $("SelectSectorSelect");
  select.empty();
  var term = $("SelectSectorFilter").get("value").toLowerCase();

  var sectors = $A(Global.MapNames);
  sectors.sort();

  sectors.each(function(sector)
  {
    if (sector.toLowerCase().search(term) != -1)
    {
      new Element("option", { value: sector, html: sector })
        .addEvent("click", function() { $("SelectSectorName").set("value", this.value); })
        .addEvent("dblclick", function() { SelectSectorWindow.Select(); })
        .inject(select, "bottom");
    }
  });
}

/*
Function: Select
Called when the user selects a sector. It also takes care of getting the code from the server.
*/
SelectSectorWindow.Select = function()
{
  if (!$chk($("SelectSectorSelect").getSelected()) || !this.Validator.validate())
    return;

  var sector = $("SelectSectorSelect").getSelected()[0].get("value");

  Utility.Server.GetSectorCode(sector, (function(result)
  {
    if (result == false)
    {
      ShowAlert("Sector not found in database.");
    }
    else
    {
      var sector = new Sector(this.name, this.sector, Global.CachedMaps[this.sector]);
      SelectSectorWindow.Callback(sector);
    }

    SelectSectorWindow.Hide();
  }).bind({ sector: sector, name: $("SelectSectorName").get("value") }));
}